package com.yupi.yupao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yupao.common.ErrorCode;
import com.yupi.yupao.exception.BusinessException;
import com.yupi.yupao.mapper.TagMapper;
import com.yupi.yupao.model.domain.Tag;
import com.yupi.yupao.model.domain.User;
import com.yupi.yupao.model.request.TagUpdateRequest;
import com.yupi.yupao.service.TagService;
import com.yupi.yupao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 张莹莹
* @description 针对表【tag(标签)】的数据库操作Service实现
* @createDate 2024-03-27 16:28:24
*/
@Service
@Slf4j
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService {
private static final String tagLog="标签业务层-";
    @Resource
    private TagMapper tagMapper;
    @Resource
    private UserService userService;
    /**
     * 添加标签
     * @param tag
     * @return
     */
    @Override
    public long addTag(Tag tag) {
        log.info(tagLog+"添加标签方法-开始调用添加标签方法.....");
        if(tag==null){
            log.info(tagLog+"添加标签方法-开始调用添加标签方法.....");
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //参数规则
         Long parentId = tag.getParentId();
        if(parentId==null||parentId<=0){
            log.info(tagLog+"添加标签方法-添加的标签是父标签");
            tag.setIsParent(1);
        }
        int t = tagMapper.insert(tag);
        if(t==0){
            log.error(tagLog+"添加标签方法-插入标签信息到数据库失败");
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"插入标签失败");
        }
        Long id = tag.getId();
        log.info(tagLog+"添加标签方法-结束调用添加标签方法.....");
        return id;
    }

    @Override
    public boolean updateTag(TagUpdateRequest tagUpdateRequest, User loginUser) {
       log.info(tagLog+"修改标签-开始修改标签业务层......");
        if(tagUpdateRequest==null){
            log.error(tagLog+"修改标签-请求参数为空");
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数为空");
        }
        if(loginUser==null){
            log.error(tagLog+"修改标签-未登录");
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        Long id = tagUpdateRequest.getId();
        if(id==null||id<=0){
            log.error(tagLog+"修改标签-标签id未传/id<=0");
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"id不符合规则");
        }
        //如果不是父标签，那么要么父标签栏之前不是null，要么父标签要改成不是null。
        Tag tag = this.getById(id);
       //1.	查看原先的parentId—》如果不为空，那么isParent不可修改为0
        //如果parentId为空，且当前没有修改parentId不为空，那么isParent不可修改为1
        //如果ParentId为空，请求参数修改ParentId不为空，那么isParent必须修改为0
       //TODO:父标签这边参数校验规则是怎么样的？
        Long userId = tag.getUserId();
        if(!userService.isAdmin(loginUser)&&(userId!=loginUser.getId())){
            log.error(tagLog+"修改标签-当前登录用户没有权限修改");
            throw new BusinessException(ErrorCode.NO_AUTH);
        }

        Tag tag1 = new Tag();
        BeanUtils.copyProperties(tagUpdateRequest,tag1);
        boolean b = this.updateById(tag1);
        if(!b){
            log.error(tagLog+"修改标签-修改标签表失败");
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"修改标签表数据库失败");
        }
        log.info(tagLog+"修改标签-结束修改标签业务层......");
        return b;
    }

    @Override
    public boolean deleteTag(long id, User loginUser) {
        log.info(tagLog+"删除标签-开始删除标签业务层......");
        //参数判空
        if(id<=0){
            log.info(tagLog+"修改标签-id不合法");
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(loginUser==null){
            log.info(tagLog+"修改标签-未登录");
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
         long id1 = loginUser.getId();
        Tag tag = this.getById(id);
        log.info(tagLog+"删除标签-根据id获取这个标签对象");
        if(!userService.isAdmin(loginUser)&&id1!=tag.getUserId()){
            log.error(tagLog+"修改标签-没有删除权限");
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        boolean b = this.removeById(id);
        if(!b){
            log.error(tagLog+"修改标签-删除标签表数据库中这条数据失败");
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"删除这条标签失败");
        }
        log.info(tagLog+"删除标签-从标签表中删除成功");
        log.info(tagLog+"删除标签-结束删除标签业务层......");
        return b;
    }
}




